﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mighty.on;

namespace Mighty.Mighty
{
    public partial class CreateRoomForm : Form
    {
        OnlinePlayer myInfo = new OnlinePlayer();
        Service webService = new Service();


        public CreateRoomForm()
        {
            InitializeComponent();
            cmbSeed.SelectedIndex = 0;
        }

        public CreateRoomForm(OnlinePlayer op)
        {
            InitializeComponent();
            myInfo = op;
        }

        private void chkLocked_CheckedChanged(object sender, EventArgs e)
        {
            if (chkLocked.Checked)
                txtPassword.Enabled = true;
            else txtPassword.Enabled = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            int roomNo = 0;

            if (txtTitle.Text == "")
            {
                MessageBox.Show("방 제목을 입력해주세요.");
                txtTitle.Focus();
                return;
            }

            if (cmbSeed.Text == "")
            {
                MessageBox.Show("시드머니를 선택해주세요.");
                txtPassword.Focus();
                return;
            }


            if (chkLocked.Checked && txtPassword.Text == "")
            {
                MessageBox.Show("비밀번호를 입력해주세요.");
                txtPassword.Focus();
                return;
            }
            try
            {
                roomNo = webService.CreateRoom(txtTitle.Text, myInfo.id, myInfo.ip, myInfo.port, Convert.ToInt32(cmbSeed.Text), txtPassword.Text);
               
                if (roomNo == -1) 
                {
                    MessageBox.Show("더이상 새로운 방을 만들 수 없습니다.");
                    this.Close();
                }
                else
                {
                    Mighty m = new Mighty();
                    this.Visible = false;

                    webService.JoinRoom(roomNo, myInfo.ip, myInfo.port, myInfo.id);

                    m.ShowDialog();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Close();
            }


        }




    }
}
